Sometimes you may need to check the type of data a user has typed into a text box. For example, suppose youre expecting the user to type in a date or some numeric data. Well, a text box control accepts all characterstheres no automatic way to mask the data so that only a certain type of data comes in. Therefore, you may need to check the data to make sure the user input the correct type. You can do this with a couple of functions presented in this section as shown in Figure 10.4.
Figure 10.4
Test data type example.
Examples Filename: TestDataType.frm
IsNumeric( )
The IsNumeric() function will return True if the value passed to it can be converted to a number. Heres an example:
Private Sub cmdIsNumeric_Click()
If IsNumeric(txtNumeric.Text) Then
MsgBox "Value is Numeric"
End If
End Sub
IsDate( )
IsDate() returns True if the value passed to it is a valid date. Heres an example:
Private Sub cmdIsDate_Click()
If IsDate(txtDate.Text) Then
MsgBox "Value is a Date"
End If
End Sub
CAUTION When IsDate() is combined with CVDate(), Visual Basic can consider a string such as 334466 as a valid date. Make sure you always check variant and strings with IsDate() prior to converting them.